﻿#region File Description

//-----------------------------------------------------------------------------
// DatabasePopulator.cs
//
// Author: ITU Team3 (2012)
//-----------------------------------------------------------------------------

#endregion File Description

#region Using Statements

using System;
using System.Collections.Generic;
using System.Diagnostics;
using rentit03_server.Business;

#endregion Using Statements

namespace rentit03_server.Database
{
    public class DatabasePopulator
    {
        private static readonly DataAccessObject Dao = new DataAccessObject();

        public static void CreatePermissions()
        {
            foreach (PermissionEnum value in Enum.GetValues(typeof(PermissionEnum)))
            {
                var permission = new RentitPermission
                {
                    Id = (int)value,
                    Name = Enum.GetName(typeof(PermissionEnum), value)
                };
                if (Dao.ReadPermission(permission.Id) == null)
                    Dao.CreatePermission(permission);
            }
        }

        public static void CreateRoles()
        {
            // Create anonymous user
            var role = new RentitRole();
            if (Dao.ReadRole("Anonymous User") == null)
            {
                role.Name = "Anonymous User";
                var perms = new List<PermissionEnum>
                               {
                                   PermissionEnum.CreateUser,
                                   PermissionEnum.ReadUser,
                                   PermissionEnum.ReadMedia,
                                   PermissionEnum.OverviewMedia
                               };

                int rId = Dao.CreateRole(role);

                foreach (var permissionEnum in perms)
                {
                    var p = Dao.ReadPermission(permissionEnum.ToString());
                    Dao.AddPermissionToRole(rId, p.Id);
                }
            }

            // Create authorized user
            if (Dao.ReadRole("Authorized User") == null)
            {
                role.Name = "Authorized User";
                var perms = new List<PermissionEnum>
                                  {
                                      PermissionEnum.CreateUser,
                                      PermissionEnum.ReadUser,
                                      PermissionEnum.ReadBasicUserInfo,
                                      PermissionEnum.ReadMedia,
                                      PermissionEnum.OverviewMedia,
                                      PermissionEnum.CreateComment,
                                      PermissionEnum.ReadComment
                                  };

                int rId = Dao.CreateRole(role);

                foreach (var permissionEnum in perms)
                {
                    var p = Dao.ReadPermission(permissionEnum.ToString());
                    Dao.AddPermissionToRole(rId, p.Id);
                }
            }

            // Create media editor
            if (Dao.ReadRole("Media editor") == null)
            {
                role.Name = "Media editor";
                var perms = new List<PermissionEnum>
                                  {
                                      PermissionEnum.CreateMedia,
                                      PermissionEnum.ReadMedia,
                                      PermissionEnum.EditMedia,
                                      PermissionEnum.DeleteMedia,
                                      PermissionEnum.OverviewMedia,
                                      PermissionEnum.EditComment,
                                      PermissionEnum.DeleteComment
                                  };
                int rId = Dao.CreateRole(role);

                foreach (var permissionEnum in perms)
                {
                    var p = Dao.ReadPermission(permissionEnum.ToString());
                    Dao.AddPermissionToRole(rId, p.Id);
                }
            }

            // Create admin
            role = new RentitRole { Name = "Admin" };
            if (Dao.ReadRole(role.Name) == null)
            {
                var perms = new List<PermissionEnum>
                    {
                        PermissionEnum.CreateUser,
                        PermissionEnum.ReadUser,
                        PermissionEnum.ReadBasicUserInfo,
                        PermissionEnum.ReadAllUserInfo,
                        PermissionEnum.EditUser,
                        PermissionEnum.DeleteUser,
                        PermissionEnum.OverviewUser,
                        PermissionEnum.ReadUserCredits,
                        PermissionEnum.EditUserCredits,
                        PermissionEnum.CreateMedia,
                        PermissionEnum.ReadMedia,
                        PermissionEnum.EditMedia,
                        PermissionEnum.DeleteMedia,
                        PermissionEnum.OverviewMedia,
                        PermissionEnum.OverviewMediaVersions,
                        PermissionEnum.EditVersion,
                        PermissionEnum.ReadUserPermissions,
                        PermissionEnum.ReadUserRole,
                        PermissionEnum.CreateUserRole,
                        PermissionEnum.DeleteUserRole,
                        PermissionEnum.CreateUserPermission,
                        PermissionEnum.EditUserPermission,
                        PermissionEnum.DeleteUserPermission,
                        PermissionEnum.OverviewUserPermission,
                        PermissionEnum.EditComment,
                        PermissionEnum.DeleteComment
                    };
                int rId = Dao.CreateRole(role);

                foreach (var permissionEnum in perms)
                {
                    var p = Dao.ReadPermission(permissionEnum.ToString());
                    Dao.AddPermissionToRole(rId, p.Id);
                }
            }
        }

        public static void CreateUsers()
        {
            // Create admin
            var user = new User
            {
                Name = "Rentit Admin",
                Email = "rentit@itu.dk",
                Address = "Rued Langgaardsvej 7, 2300 Kbh S",
                PhoneNumber = "33123456",
                Password = SessionHandler.EncryptPassword("vandamme"),
                Created = DateTime.Now,
                Credits = 1000000,
                Updated = DateTime.Now
            };
            var roles = new List<string>
            {
                "Admin"
            };
            int uId = Dao.CreateUser(user);

            foreach (var role in roles)
            {
                var r = Dao.ReadRole(role);
                Debug.Assert(Dao.AddRoleToUser(r.Id, uId));
            }

            // Create tester
            user = new User
            {
                Name = "Tester Testersen",
                Email = "test@test.dk",
                Address = "Testgade 10",
                PhoneNumber = "73577357",
                Password = SessionHandler.EncryptPassword("test"),
                Created = DateTime.Now,
                Credits = 100,
                Updated = DateTime.Now
            };
            roles = new List<string>
            {
                "Authorized User"
            };
            uId = Dao.CreateUser(user);

            foreach (var role in roles)
            {
                var r = Dao.ReadRole(role);
                Debug.Assert(Dao.AddRoleToUser(r.Id, uId));
            }

            // Create tester
            user = new User
            {
                Name = "Editor Editorsen",
                Email = "editor@test.dk",
                Address = "Editorgade 10",
                PhoneNumber = "30170411",
                Password = SessionHandler.EncryptPassword("editor"),
                Created = DateTime.Now,
                Credits = 10000,
                Updated = DateTime.Now
            };
            roles = new List<string>
            {
                "Media editor",
                "Authorized User"
            };
            uId = Dao.CreateUser(user);

            foreach (var role in roles)
            {
                var r = Dao.ReadRole(role);
                Debug.Assert(Dao.AddRoleToUser(r.Id, uId));
            }
        }
    }
}